package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.VehicleInfo;
import com.ruoyi.system.domain.VehicleUsage;
import com.ruoyi.system.mapper.VehicleUsageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DriverInfoMapper;
import com.ruoyi.system.domain.DriverInfo;
import com.ruoyi.system.service.IDriverInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 驾驶员信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-05
 */
@Service
public class DriverInfoServiceImpl implements IDriverInfoService 
{
    @Autowired
    private DriverInfoMapper driverInfoMapper;
    @Autowired
    private VehicleUsageMapper vehicleUsageMapper;
    /**
     * 查询驾驶员信息
     * 
     * @param id 驾驶员信息ID
     * @return 驾驶员信息
     */
    @Override
    public DriverInfo selectDriverInfoById(Long id)
    {
        return driverInfoMapper.selectDriverInfoById(id);
    }

    /**
     * 查询驾驶员信息列表
     * 
     * @param driverInfo 驾驶员信息
     * @return 驾驶员信息
     */
    @Override
    public List<DriverInfo> selectDriverInfoList(DriverInfo driverInfo)
    {
        return driverInfoMapper.selectDriverInfoList(driverInfo);
    }

    /**
     * 新增驾驶员信息
     * 
     * @param driverInfo 驾驶员信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertDriverInfo(DriverInfo driverInfo)
    {
        //驾驶员编号
        String driverNumber = driverInfo.getDriverNumber();
        DriverInfo query = new DriverInfo();
        query.setDriverNumber(driverNumber);

        //如果数据库有这条数据，代表该驾驶员编号已经存在，则不能新增
        DriverInfo selectDriverInfo = driverInfoMapper.selectDriverInfo(query);
        if(!Objects.isNull(selectDriverInfo))
        {
            throw new BusinessException("该驾驶员编号已存在，请你重新输入!");
        }
        //设置基本信息
        driverInfo.setIsDeleted(0L);
        driverInfo.setCreateBy(ShiroUtils.getLoginName());
        driverInfo.setCreateTime(new Date());
        driverInfo.setUpdateBy(ShiroUtils.getLoginName());
        driverInfo.setUpdateTime(new Date());
        return driverInfoMapper.insertDriverInfo(driverInfo);
    }

    /**
     * 修改驾驶员信息
     * 
     * @param driverInfo 驾驶员信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateDriverInfo(DriverInfo driverInfo)
    {
        Long id = driverInfo.getId();
        DriverInfo driverInfoById = driverInfoMapper.selectDriverInfoById(id);
        //驾驶员编号
        String driverNumber = driverInfo.getDriverNumber();
        DriverInfo query = new DriverInfo();
        query.setDriverNumber(driverNumber);

        //如果数据库有这条数据，代表该驾驶员编号已经存在，则不能更新
        DriverInfo selectDriverInfo = driverInfoMapper.selectDriverInfo(query);
        if(!Objects.isNull(selectDriverInfo)&&!Objects.equals(driverNumber,driverInfoById.getDriverNumber()))
        {
            throw new BusinessException("该驾驶员编号已存在，请你重新输入!");
        }
        driverInfo.setUpdateBy(ShiroUtils.getLoginName());
        driverInfo.setUpdateTime(new Date());
        return driverInfoMapper.updateDriverInfo(driverInfo);
    }

    /**
     * 删除驾驶员信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteDriverInfoByIds(String ids)
    {
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            DriverInfo driverInfo = driverInfoMapper.selectDriverInfoById(Long.valueOf(id));
            if(Objects.isNull(driverInfo))
            {
                //已删除直接跳过
                continue;
            }
            //驾驶员编号
            String driverNumber = driverInfo.getDriverNumber();
            VehicleUsage queryVehicleUsage = new VehicleUsage();
            queryVehicleUsage.setDriverNumber(driverNumber);
            queryVehicleUsage.setStatus(1L);//查正在使用的车辆
            List<VehicleUsage> vehicleUsageList = vehicleUsageMapper.selectVehicleUsageList(queryVehicleUsage);

            if(vehicleUsageList!=null && vehicleUsageList.size()>0)
            {
                throw new BusinessException("驾驶员编号为"+driverInfo.getDriverNumber()+"驾驶员正在使用车辆，请先归还车辆!");
            }
        }


        return driverInfoMapper.deleteDriverInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除驾驶员信息信息
     * 
     * @param id 驾驶员信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteDriverInfoById(Long id)
    {
        DriverInfo driverInfo = driverInfoMapper.selectDriverInfoById(id);
        if(Objects.isNull(driverInfo))
        {
            throw new BusinessException("该驾驶员已删除!");
        }
        //驾驶员编号
        String driverNumber = driverInfo.getDriverNumber();
        VehicleUsage queryVehicleUsage = new VehicleUsage();
        queryVehicleUsage.setDriverNumber(driverNumber);
        queryVehicleUsage.setStatus(1L);//查正在使用的车辆
        List<VehicleUsage> vehicleUsageList = vehicleUsageMapper.selectVehicleUsageList(queryVehicleUsage);

        if(vehicleUsageList!=null && vehicleUsageList.size()>0)
        {
            throw new BusinessException("该驾驶员正在使用车辆，请先归还车辆!");
        }

        return driverInfoMapper.deleteDriverInfoById(id);
    }
}
